//
//  ServiceViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/24.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "ServiceViewController.h"
#import "ServiceDetailsViewController.h"
#import "OptionButton.h"
@interface ServiceViewController ()
@property(nonatomic,strong) NSArray * optionButtonArray;//操作按钮数组
@property(nonatomic, strong)  NSMutableArray * optionButtons;
@property(nonatomic,strong)UIView* buttonBgView;//


@end

@implementation ServiceViewController

//视图将要显示时隐藏
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

//视图将要消失时取消隐藏
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self customNaviItemTitle:@"方便服务" isLeft:NO];
    // [self setLeftBtnWithString:nil]; 
    [self createView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)createView{
    
    
    WeakSelf;
    //创建背景视图
    self.buttonBgView = [[UIView alloc]init];
    self.view.backgroundColor = RGBA(243, 248, 248, 1);
    [self.view addSubview:self.buttonBgView];
    //关闭停靠模式
    self.buttonBgView.translatesAutoresizingMaskIntoConstraints=NO;
    [self.buttonBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@300);
        make.top.equalTo(weakSelf.view.mas_top).offset (46.0f);
    }];
    self.buttonBgView.backgroundColor = RGBA(243, 248, 248, 1);
    
    
    
    //创建更多选项按钮
    
    NSArray * optionButtonNames = @[@"读报",@"快递查询",@"烘焙达人",@"航班查询",@"模拟驾考",@"吃客汇"];
    
    NSArray * optionButtonImages = @[@"service_read_default",@"service_express_default",@"service_baking_default",@"service_flight_default",@"service_driving_default",@"service_food_default"];
    
    
    
    //定义选项按钮的高度和宽度
    CGFloat optionButtonWidth = ScreenWidth/3;
    CGFloat optionButtonHeight = ScreenWidth/3;
    
    
    
    //定义存取选项按钮
    _optionButtons = [NSMutableArray array];
    
    [optionButtonNames enumerateObjectsUsingBlock:^(NSString *buttonName, NSUInteger idx, BOOL *stop) {
        //九宫格布局 取余 取整
        NSString * imageName = optionButtonImages[idx];
        
        OptionButton * opBtn1 = [[OptionButton alloc] initWithTitle:buttonName image:[UIImage imageNamed:imageName] frame:CGRectMake((idx % 3)*optionButtonWidth, (idx / 3) * optionButtonHeight , optionButtonWidth, optionButtonHeight)];
        
        opBtn1.button.translatesAutoresizingMaskIntoConstraints = NO;
        // 建立约束
        [opBtn1.button mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.height.equalTo(opBtn1.button.mas_width);
        }];
        
        opBtn1.backgroundColor = RGBA(243, 248, 248, 1);
        opBtn1.layer.shadowOpacity = 0.;
        opBtn1.layer.shadowOffset = CGSizeMake(0, 0);
        [self.buttonBgView addSubview:opBtn1];
        
        [_optionButtons addObject:opBtn1];
        opBtn1.button.tag=idx +1000 ;
        
        [opBtn1.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }];
    
    
}

-(void)buttonAction:(UIButton *)sender{
    
     NSArray * optionButtonNames = @[@"读报",@"快递查询",@"烘焙达人",@"航班查询",@"模拟驾考",@"吃客汇"];
    ServiceDetailsViewController * sevrice = [[ServiceDetailsViewController alloc]init];
    sevrice.titleStr = optionButtonNames[sender.tag-1000];
    sevrice.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:sevrice animated:YES];
    
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
